package org.bpms.controller.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bpms.model.admin.Smenu;
import org.bpms.model.admin.Suser;
import org.bpms.service.admin.LogService;
import org.bpms.service.admin.MenuService;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.jfinal.aop.Enhancer;
import com.jfinal.core.Controller;

/**
 * 菜单控制器
 * @author hupei
 *
 */
public class MenuController  extends Controller{
	private MenuService menuService = Enhancer.enhance(MenuService.class);
	private LogService  logService = Enhancer.enhance(LogService.class);
	/**
	 * 默认菜单首页
	 */
	public void index(){
		List<Smenu> smenus = new ArrayList<Smenu>();
		Smenu menu = new Smenu();
		//为获取顶层菜单设置id、pid
		menu.set("id", null);
		menu.set("pid", null);
		menuService.allMenu(smenus, menu);
		setAttr("smenus", smenus);
		render("/admin/menu/menu.html");
	}
	
	/**
	 * 获取角色拥有的菜单
	 */
	public void menuFromRole(){
		String roleId = getPara("roleid");//角色id
		List<Smenu> smenus = new ArrayList<Smenu>();
		Smenu menu = new Smenu();
		//为获取顶层菜单设置id、pid
		menu.set("id", null);
		menu.set("pid", null);
		menuService.menuFromRole(smenus, roleId, menu);
		setAttr("rolemenus", smenus);
		setAttr("roleid", roleId);
		render("/admin/role/role_getmenu.html");
	}
	/**
	 * 单个保存
	 */
	public void save(){
		Map<String, Object> map = new HashMap<String , Object>();
		Smenu menu = getModel(Smenu.class);
		menuService.saveOrUpdate(menu);
		Suser user = getSessionAttr("user");
		logService.create(user.getStr("username"), "新增菜单:"+menu.getStr("text"));
		map.put("success", true);
		map.put("obj", menu);
		renderJson(map);
	}
	
	/**
	 * 在树形菜单上点击保存
	 */
	public void saveTree(){
		String menus = getPara("menus");
		ObjectMapper mapper = new ObjectMapper();
		Map<String, Object> jsonmap = new HashMap<String , Object>();
		mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		Map map = null;
		try {
			map = mapper.readValue(menus, Map.class);
			if (null != map){
				menuService.saveTree(map);
				Suser user = getSessionAttr("user");
				logService.create(user.getStr("username"), "调整菜单");
			}
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jsonmap.put("success", false);
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jsonmap.put("success", false);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jsonmap.put("success", false);
		}
		jsonmap.put("success", true);
		renderJson(jsonmap);
	}
	
	/**
	 * 删除菜单
	 */
	public void del(){
		Map<String, Object> map = new HashMap<String , Object>();
		Smenu menu = new Smenu();
		String id = getPara("id");
		menu = menuService.findById(id);
		menu.set("id", id);
		Suser user = getSessionAttr("user");
		logService.create(user.getStr("username"), "删除菜单:"+menu.getStr("text"));
		menuService.delMenu(menu);
		map.put("success", true);
		renderJson(map);
	}
}
